package com.baosight.risk.web.riaa;

import com.baosight.risk.api.riaa.RiquInquiryCompService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.ExcelUtil;
import com.baosight.risk.service.riaa.entity.RiquInquiryComp;
import com.baosight.risk.service.riaa.vo.RiquInquiryCompVO;
import com.baosight.risk.service.riaa.vo.RiquInquiryDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * 类 名: RiaaQuesFillInController
 * 描 述: 问卷填报
 * 作 者: 黎秋荣
 * 创 建：2019年06月05日
 * 版 本：v2.2.0
 * 历 史: (版本) 作者 时间 注释
 */
@Slf4j
@RestController
@RequestMapping("/riqu/inquiry")
@Api(value = "问卷填报", description = "问卷填报", produces = MediaType.APPLICATION_JSON)
public class RiquInquiryCompController extends BaseController {

    @Autowired
    private RiquInquiryCompService inquiryCompService;

    @RequestMapping("selectList")
    @ApiOperation("问卷填报列表")
    public AjaxResult selectList(RiquInquiryComp inquiryComp) {
//        startPage();
        return getResultPage(inquiryCompService.selectList(inquiryComp));
    }

    @GetMapping("getById")
    @ApiOperation("问卷填报列表")
    public AjaxResult getById(String id) {
        return success(inquiryCompService.getById(id));
    }

    /**
     * 根据IDS查询问卷填报
     *
     * @return
     */
    @PostMapping("/getByIds")
    @ApiOperation("根据IDS查询问卷填报")
    public AjaxResult getByIds(@RequestBody List<String> ids) {
        return getResultPage(inquiryCompService.getByIds(ids));
    }


    @GetMapping("quesAnalysis")
    @ApiOperation("问卷分析")
    public AjaxResult quesAnalysis(@RequestParam("quId") String quId) {
//        startPage();
        return getResultPage(inquiryCompService.quesAnalysis(quId));
    }

    @RequestMapping("queryInquiryInfo")
    @ApiOperation("问卷填报情况查询")
    public AjaxResult queryInquiryInfo(RiquInquiryComp comp){
        log.info("问卷填报情况查询参数：" + comp.toString());
        return getResultPage(inquiryCompService.queryInquiryInfo(comp));
    }


    @PostMapping("saveInquiry")
    @Log(title = "问卷填报修改", businessType = BusinessType.UPDATE)
    @ApiOperation("问卷填报修改")
    public AjaxResult saveInquiry(@RequestBody RiquInquiryCompVO compVO) {
        if (CollectionUtils.isEmpty(compVO.getRiquInquiryDetail()) &&
                CollectionUtils.isEmpty(compVO.getRiquInquiryBGTADetail())) {
            log.error("问卷选项数据为空，不能保存！");
            return error("问卷选项数据为空，不能保存！");
        }
        return success(inquiryCompService.saveInquiry(compVO, getUser()));
    }

    @GetMapping("deleteDetailById")
    @Log(title = "删除问卷详细", businessType = BusinessType.DELETE)
    @ApiOperation("删除问卷详细")
    public AjaxResult deleteDetailById(@RequestParam("id") String id) throws Exception {
        return toAjax(inquiryCompService.deleteDetailById(id));
    }

    @PostMapping("submit")
    @Log(title = "提交问卷调研")
    @ApiOperation("提交问卷调研")
    public AjaxResult saveSubmit(@RequestBody RiquInquiryCompVO compVO) {
        if (CollectionUtils.isEmpty(compVO.getRiquInquiryDetail())) {
            log.error("问卷选项数据为空，不能保存！");
            return error("问卷选项数据为空，不能保存！");
        }
        return toAjax(inquiryCompService.saveSubmit(compVO, getUser()));
    }

    @PostMapping("export")
    @ResponseBody
    @ApiOperation("导出问卷分析")
    public AjaxResult exportExcel(@RequestBody RiquInquiryDetailVO detailVO) {
        log.info("导出问卷分析参数：" + detailVO.toString());
        ExcelUtil<RiquInquiryDetailVO> excel = new ExcelUtil<>(RiquInquiryDetailVO.class);
        return excel.exportExcel(inquiryCompService.quesAnalysis(detailVO.getQuId()), "问卷分析",null);
    }

}
